package cn.iocoder.yudao.module.expert.service.extractinfo;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.expert.dal.dataobject.expertinfo.ExpertInfoDO;
import cn.iocoder.yudao.module.expert.dal.mysql.expertinfo.ExpertInfoMapper;
import cn.iocoder.yudao.module.expert.dal.mysql.projectinfo.ProjectInfoMapper;
import jodd.util.StringUtil;
import org.apache.ibatis.executor.BatchResult;
import org.apache.poi.ss.formula.functions.T;
import org.checkerframework.checker.units.qual.K;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.iocoder.yudao.module.expert.controller.admin.extractinfo.vo.*;
import cn.iocoder.yudao.module.expert.dal.dataobject.extractinfo.ExtractInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.expert.dal.mysql.extractinfo.ExtractInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.expert.enums.ErrorCodeConstants.*;

/**
 * 抽签结果 Service 实现类
 *
 * @author 周天贺
 */
@Service
@Validated
public class ExtractInfoServiceImpl implements ExtractInfoService {

    @Resource
    private ExtractInfoMapper extractInfoMapper;
    @Resource
    private ExpertInfoMapper infoMapper;
    @Resource
    private ProjectInfoMapper projectInfoMapper;
    @Override
    @Transactional
    public Long createExtractInfo(ExtractInfoSaveReqVO createReqVO) {
        // 插入
        Integer extractExpertNum=createReqVO.getExtractExpertNum();
        createReqVO.setExtractTime(LocalDateTime.now());
        if(createReqVO.getExtractExpertNum()>=createReqVO.getExpertIdList().size()){
            createReqVO.setExtractExpertNum(createReqVO.getExtractExpertNum()-createReqVO.getExpertIdList().size());
        }else{
            createReqVO.setExtractExpertNum(0);
        }
        if(extractExpertNum<createReqVO.getExpertIdList().size()){
            Integer delnum=createReqVO.getExpertIdList().size()-extractExpertNum; //所填抽取人数比固定专家数少时，随机删除多余固定专家
            Collections.shuffle(createReqVO.getExpertIdList());
            for (int i = 0; i < delnum; i++) {
                createReqVO.getExpertIdList().remove(0); // 删除前N个元素
            }
        }
        List<ExpertInfoDO> list=infoMapper.selectRandom(createReqVO);
        if(StringUtil.isEmpty(createReqVO.getExtractMajor())){
            createReqVO.setExtractMajor("不限专业");
        }

        List<ExtractInfoDO> insertList=new ArrayList<>();
        for(ExpertInfoDO expertInfoDO:list){
            ExtractInfoDO extractInfo = BeanUtils.toBean(createReqVO, ExtractInfoDO.class);
            extractInfo.setExpertId(expertInfoDO.getId());
            extractInfo.setExpertIden(expertInfoDO.getExpertIden());
            extractInfo.setExpertName(expertInfoDO.getExpertName());
            extractInfo.setExpertMajor(expertInfoDO.getExpertMajor());
            extractInfo.setExpertTitle(expertInfoDO.getExpertTitle());
            extractInfo.setExpertPhone(expertInfoDO.getExpertPhone());
            extractInfo.setExpertUnit(expertInfoDO.getExpertUnit());
            extractInfo.setExtractExpertNum(extractExpertNum);
            extractInfo.setMajor(expertInfoDO.getMajor());
            extractInfo.setRemark(expertInfoDO.getRemark());
            extractInfo.setIdNumber(expertInfoDO.getIdNumber());
//            if(!CollectionUtils.isAnyEmpty(createReqVO.getExpertIdList())&&createReqVO.getExpertIdList().contains(expertInfoDO.getId())) {
//                extractInfo.setRemark("1");
//            }
            insertList.add(extractInfo);
        }
        extractInfoMapper.insert(insertList);
        projectInfoMapper.updateExtractBatchNumById(createReqVO.getProjectId());
        // 返回
        return 1L;
    }

    @Override
    public void updateExtractInfo(ExtractInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateExtractInfoExists(updateReqVO.getId());
        // 更新
        ExtractInfoDO updateObj = BeanUtils.toBean(updateReqVO, ExtractInfoDO.class);
        extractInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteExtractInfo(Long id) {
        // 校验存在
        validateExtractInfoExists(id);
        // 删除
        extractInfoMapper.deleteById(id);
    }

    private void validateExtractInfoExists(Long id) {
        if (extractInfoMapper.selectById(id) == null) {
            throw exception(EXTRACT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ExtractInfoDO getExtractInfo(Long id) {
        return extractInfoMapper.selectById(id);
    }

    @Override
    public PageResult<ExtractInfoDO> getExtractInfoPage(ExtractInfoPageReqVO pageReqVO) {
        return extractInfoMapper.selectPage(pageReqVO);
    }
}